package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.mapper.TbProdImageMapper;
import com.bwie.mapper.TbProdMapper;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.poi.ExcelUtil;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdImages;
import com.bwie.pojo.TbType;
import com.bwie.service.TbProdService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.val;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * description:
 *
 * @Author caoshuaifeng
 * @Create 2025/3/3 9:35
 * @Version 1.0
 */
@Service
public class TbProdServiceImpl implements TbProdService {

    @Autowired
    TbProdMapper tbProdMapper;
    @Autowired
    TbProdImageMapper tbProdImageMapper;
    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Autowired
    TbTypeMapper tbTypeMapper;
    @Override
    public R getProdList(PageInfoVo pageInfoVo) {
        Page<TbProd> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<TbProd> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbProd::getProdName,pageInfoVo.getKeyWord());
        }
        Page<TbProd> tbProdPage = tbProdMapper.selectPage(page, wrapper);
        return R.OK(tbProdPage);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R addProd(TbProd tbProd) {
        QueryWrapper<TbProd> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbProd::getProdName,tbProd.getProdName());
        if (tbProdMapper.selectOne(wrapper)!=null){
            return R.ERROR(500,"商品名称已存在");
        }
         tbProdMapper.insert(tbProd);
         if(tbProd.getImgList()!=null && tbProd.getImgList().size()>0){
         for (String url : tbProd.getImgList()) {
             TbProdImages tbProdImages = new TbProdImages();
             tbProdImages.setProdId(tbProd.getProdId());
             tbProdImages.setImageUrl(url);
             tbProdImageMapper.insert(tbProdImages);
         }
         }
        return R.OK();
    }
    @Override
    public R uploadFile(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath= fastFileStorageClient.uploadFile(file.getInputStream(),file.getSize(), extName,null);
            String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            return R.ERROR(500,"上传失败");

        }
    }
    @Override
    public R getProdById(Integer prodId) {
        TbProd tbProd = tbProdMapper.selectById(prodId);
        if(tbProd==null){
            return R.ERROR(500,"商品不存在");
        }
        TbProd tbProdId = tbProdMapper.selectById(prodId);
        return R.OK(tbProdId);

    }
    @Override
    public R updateProd(TbProd tbProd) {
        TbProd prod = tbProdMapper.selectById(tbProd.getProdId());
        if(prod==null){
            return R.ERROR(500,"商品不存在");
        }
        tbProdMapper.updateById(tbProd);
        return R.OK();
    }

    @Override
    public R deleteAll(ArrayList<TbProd> tbProds) {
        for (TbProd tbProd : tbProds) {
            tbProdMapper.deleteById(tbProd.getProdId());
        }
        return R.OK();
    }

    @Override
    public R getTypeList() {
        List<TbType> tbTypes = tbTypeMapper.selectList(null);
        return R.OK(tbTypes);

    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<TbProd> tbProds = tbProdMapper.selectList(null);
        ExcelUtil<TbProd> tbProdExcelUtil = new ExcelUtil<>(TbProd.class);
        tbProdExcelUtil.exportExcel(response, tbProds, "商品信息表");


    }

    @Override
    public R importExcel(MultipartFile file) {
        try {
            ExcelUtil<TbProd> tbProdExcelUtil = new ExcelUtil<>(TbProd.class);
            List<TbProd> tbProds = tbProdExcelUtil.importExcel(file.getInputStream());
            for (TbProd tbProd : tbProds) {
                tbProdMapper.insert(tbProd);
            }
            int size = tbProds.size();
            R ok = R.OK(size);
            ok.setMsg("成功导入"+size+"条数据");
            return ok;
        } catch (IOException e) {
           e.printStackTrace();
        }
        return R.ERROR(500,"导入失败");
    }

    @Override
    public R deleteProdById(Integer prodId) {
        TbProd tbProd = tbProdMapper.selectById(prodId);
        if(tbProd==null){
            return R.ERROR(500,"商品不存在");
        }
        tbProdMapper.deleteById(prodId);

        return R.OK();
    }




}
